Ruby ハッシュ
code:rb
fruits = { apple: 100, banana: 200, cherry: 300 }
fruits.has_key?(:cherry) #=> true # 要素追加・上書き
fruits #=> {:apple=>100, :banana=>200, :cherry=>300, :grape=>400} # []=のエイリアス、store
fruits.store(:grape, 400)
fruits.delete(:apple)
fruits #=> {:banana=>200, :cherry=>300} # ハッシュの展開
{ orange: 50, fruits } #=> SyntaxError { orange: 50, **fruits } #=> {:orange=>50, :apple=>100, :banana=>200, :cherry=>300} # ハッシュの結合
hash = { orange: 50 }
hash.merge(fruits) #=> {:orange=>50, :apple=>100, :banana=>200, :cherry=>300} # 配列に変換
code:rb
fruits = { apple: 100, banana: 200, cherry: 300 }
fruits.each { |key, value| puts "#{key}は#{value}円です。" }
fruits.each_key { |key| puts key }
fruits.each_value { |value| puts value }
# mapで返ってくるのは「配列」
# to_hでハッシュに変換
fruits.map { |k, v| k, v * 2 }.to_h #=> {:apple=>200, :banana=>400, :cherry=>600}